package com.goddess.yeecolor.tools;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 *okttp工具类
 */
public class OkttpUtils {

    private static OkttpUtils mOkttp;
    private OkHttpClient mOkttpClient;

    public  OkttpUtils(){
    }
    public static OkttpUtils getIntence(){
        if (null==mOkttp){
            mOkttp=new OkttpUtils();
        }
        return mOkttp;
    }




    /**
     * 没有带参数的
     * @param url
     * @param handler
     */
    public void OkttpPost(String url, Map<Object,Object>map,final Handler handler,int messageTag){
//        mOkttpClient=new OkHttpClient();

        mOkttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();
        try {
            FormBody.Builder mBody=new FormBody.Builder();
            if (map.size()>0){
                for (Map.Entry<Object,Object> m :map.entrySet()) {
                    mBody.add((String) m.getKey(), (String)m.getValue());
                }
            }
            Request mRequest=new Request.Builder().url(url).post(mBody.build()).build();
            mOkttpClient.newCall(mRequest).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("zy",e.toString());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();//response.body().string()只能调用一次
                    Log.i("zy",result.toString());

                    Message message = new Message();
                    message.what=messageTag;
                    Bundle bundle = new Bundle();
                    bundle.putString("result", result);
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            });
        }
        catch (Exception e){
            Log.i("zy",e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 上传
     */
    public void OkttpPost(String url, Handler handler, Map<Object,Object>map,int tag){
        mOkttpClient=new OkHttpClient();
        try {
            /**
             * 表单
             */
            MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
            if(map!=null)
            {
                for (Map.Entry<Object,Object>maps:map.entrySet()){
                    /**
                     * key  2.上传到服务器的图片名字   parse里面第二个参数是value
                     */
                    File f=new File((String) maps.getValue());
                    builder.addFormDataPart((String) maps.getKey(),"image.png",RequestBody.create(MediaType.parse("application/octet-stream"),f));
                }
            }
            Request request=new Request.Builder().url(url).post(builder.build()).build();
            mOkttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    LogUtils.getInstence().info(e.toString());
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();//response.body().string()只能调用一次
                    Message message = new Message();
                    message.what=tag;
                    Bundle bundle = new Bundle();
                    bundle.putString("result", result);
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            });
        }
        catch (Exception e){
            LogUtils.getInstence().info(e.toString());
            e.printStackTrace();
        }
    }

}
